package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.FinancingProduct;
import service.FinancingProductService;
import service.impl.FinancingProductServicImpl;

/**
 * Servlet implementation class AddFinProServlet
 */
@WebServlet("/AddFinProServlet")
public class AddFinProServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id =request.getParameter("id"); //获取前端的参数
		String risk =request.getParameter("risk"); 
		String income =request.getParameter("income"); 
		String saleStarting =request.getParameter("saleStarting"); 
		String saleEnd =request.getParameter("saleEnd"); 
		String end =request.getParameter("end"); 
		
		//封装数据
		FinancingProduct finPro =new FinancingProduct(id,Integer.parseInt(risk),income,saleStarting,saleEnd,end);
		
		//调用service方法执行新增
		FinancingProductService fpService = new FinancingProductServicImpl();
		int res = fpService.addFinancingProduct(finPro); //新增
		
		PrintWriter out =response.getWriter();
		
		if(res> 0) {
			out.println("	<script type='text/javascript'>" + 
					"		alert('保存成功');	window.location.href='SelectFinProServlet';</script>");
			//request.getRequestDispatcher("SelectFinProServlet").forward(request, response);  //去查询全部的servlet
		}else {
			out.println("	<script type='text/javascript'>" + 
					"		alert('保存失败');	window.location.href='addFinPro.jsp';</script>");
			//request.getRequestDispatcher("addFinPro.jsp").forward(request, response);  //去新增页面
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
